const { start } = require("repl");

//1.观察者模式
class Star{
    constructor(name){
        this.name = name;
        this.state = '';
        this.observers = [];
    };
    getState(){
        return this.state;
    };
    setState(newValue){
        this.state = newValue;
        this.notifyAllObaervers();
        return true
    };
    notifyAllObaervers(){
        if(this.observers.length>0){
            this.observers.forEach(observer=>observer.update())
        }
    };
    attach(observer){
        this.observers.push(observer);
    }
}

class Fan {
    constructor(name){
        this.name = name;
    };
    update(){
        console.log('hello 被观察者更新啦')
    }
}

let f1 = new Fan('张三');
let start1 = new Star('胡歌');
// 张三观察胡歌
start1.attach(f1);
//胡歌更新状态通知张三
start1.setState('1111');